Handle Alert Popup
Coding of Alert Popup
package asc; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ALertPopUp { public static void main(String[] args) throws InterruptedException { WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert"); driver.manage().window().maximize(); WebElement frame1 = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame1); driver.findElement(By.xpath("/html/body/button")).click(); String alertText = driver.switchTo().alert().getText(); System.out.println(alertText); Thread.sleep(2000); driver.switchTo().alert().accept(); // driver.switchTo().alert().dismiss(); driver.switchTo().parentFrame(); System.out.println(driver.getTitle()); // driver.quit(); } }

Output

In Selenium, handling alert pop-ups involves switching the WebDriver's context to the alert and then performing actions such as accepting, dismissing, or retrieving the alert's text.
Key Concepts of Alert Handling in Selenium:
1. Switching to Alert:
- To interact with an alert, you first need to switch the WebDriver's context to the alert using driver .switchTo() .alert(). This method returns an Alert object that represents the alert window.
2. Retrieving Alert Text:
- After switching to the alert, you can retrieve the text displayed in the alert using alert() .getText(). This is often used to verify the alert's content before taking further action.
3. Accepting or Dismissing the Alert:
- Accepting: To accept the alert (equivalent to clicking "OK"), use alert() .accept().
- Dismissing: To dismiss the alert (equivalent to clicking "Cancel" or closing the alert), use alert() .dismiss().
Breakdown Our Code
1. Setup and Navigation:
WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert"); driver.manage().window().maximize();
2. Handling the Iframe:
WebElement frame1 = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame1);
3. Triggering the Alert:
driver.findElement(By.xpath("/html/body/button")).click();
4. Switching to and Handling the Alert:
String alertText = driver.switchTo().alert().getText(); System.out.println(alertText);
Thread.sleep(2000);
driver.switchTo().alert().accept(); // driver.switchTo().alert().dismiss();
5. Returning to the Parent Frame:
driver.switchTo().parentFrame();
6. Printing the Page Title:
System.out.println(driver.getTitle());
7. Closing the Browser (Optional):
// driver.quit();
package asc; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class SendkeysInAlert { public static void main(String[] args) throws InterruptedException { WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt"); driver.manage().window().maximize(); WebElement frame1 = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame1); driver.findElement(By.xpath("/html/body/button")).click(); Thread.sleep(2000); Alert alertOnPage = driver.switchTo().alert(); alertOnPage.sendKeys("Akhil Chukkalwar"); alertOnPage.accept(); driver.switchTo().parentFrame(); System.out.println(driver.getTitle()); // driver.quit(); } }
This code demonstrates how to interact with a JavaScript prompt alert in Selenium WebDriver. A prompt alert allows users to input text before accepting or dismissing the alert. Here's a breakdown of the code and explanation of the key concepts:
1. Setup and Navigation:
WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt"); driver.manage().window().maximize();
WebDriverManager.chromedriver().setup();:
new ChromeDriver();
driver.get(...)
driver.manage().window().maximize();:
2. Switching to the Iframe:
WebElement frame1 = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame1);
driver.findElement(By.id("iframeResult"))
driver.switchTo().frame(frame1);:
3. Triggering the Prompt Alert:
driver.findElement(By.xpath("/html/body/button")).click();
4. Handling the Prompt Alert:
Alert alertOnPage = driver.switchTo().alert(); alertOnPage.sendKeys("Akhil Chukkalwar"); alertOnPage.accept();
Alert alertOnPage = driver.switchTo().alert();
alertOnPage.sendKeys("Akhil Chukkalwar");
alertOnPage.accept();
5. Switching Back to the Main Document:
driver.switchTo().parentFrame();
6. Retrieving and Printing the Page Title:
System.out.println(driver.getTitle());
7. Quitting the Browser (Commented Out):
// driver.quit();
Summary
Interacting with Prompts In Selenium, sendKeys() can be used with prompt alerts to input text. This is not applicable to simple alerts or confirmation dialogs, which only allow acceptance or dismissal.
Switching Context Always ensure you switch to the appropriate frame or alert before interacting with elements within them, and switch back if further actions are needed outside.